package com.majun.huaxishe.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.majun.huaxishe.constant.InfoConstant;
import com.majun.huaxishe.constant.StatusConstant;
import com.majun.huaxishe.entity.Album;
import com.majun.huaxishe.entity.Photos;
import com.majun.huaxishe.entity.dto.AlbumDTO;
import com.majun.huaxishe.entity.vo.AlbumVO;
import com.majun.huaxishe.exception.BaseException;
import com.majun.huaxishe.mapper.AlbumMapper;
import com.majun.huaxishe.service.IAlbumService;
import com.majun.huaxishe.service.IPhotosService;
import com.majun.huaxishe.service.IThemeService;
import com.majun.huaxishe.utils.HxsUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 相册表 服务实现类
 * </p>
 *
 * @author 马骏
 * @since 2024-02-29
 */
@Service
public class AlbumServiceImpl extends ServiceImpl<AlbumMapper, Album> implements IAlbumService {
    @Autowired
    private IThemeService themeService;

    @Autowired
    private IPhotosService photosService;

    @Autowired
    private AlbumMapper albumMapper;

    @Override
    public Page<AlbumVO> queryPage(AlbumDTO albumDTO) {
        Integer status = albumDTO.getStatus();
        String name = albumDTO.getName();
        Integer themeId = albumDTO.getThemeId();
        Page<AlbumVO> albumVOPage = albumMapper.queryPage(new Page<>(albumDTO.getCurrentPage(), albumDTO.getPageSize()), status, name, themeId);
        List<Integer> albumIds = albumVOPage.getRecords().stream().map(AlbumVO::getId).collect(Collectors.toList());
        Map<Integer, Integer> countMap = photosService.countByAlbumId(albumIds);
        List<AlbumVO> result = albumVOPage.getRecords().stream().peek(a -> a.setPhotosCount(countMap.get(a.getId()))).collect(Collectors.toList());
        albumVOPage.setRecords(result);
        return albumVOPage;
    }

    @Override
    public List<AlbumVO> queryByThemeId(Integer themeId) {
        List<Album> list = lambdaQuery()
                .eq(Album::getThemeId, themeId)
                .eq(Album::getStatus, StatusConstant.SHOW)
                .orderByDesc(Album::getWeight)
                .list();
        List<Integer> albumIds = list.stream().map(Album::getId).collect(Collectors.toList());
        //查询每个相册的照片数
        Map<Integer, Integer> countMap = photosService.countByAlbumId(albumIds);
        return list.stream().map(a ->{
            AlbumVO albumVO = new AlbumVO();
            BeanUtils.copyProperties(a,albumVO);
            return albumVO;
        }).peek(a -> a.setPhotosCount(countMap.get(a.getId()))).collect(Collectors.toList());
    }
    @Transactional
    @Override
    public void addAlbum(AlbumDTO albumDTO) {
        if (HxsUtils.isEmpty(albumDTO.getPhotos())){
            throw new BaseException(InfoConstant.NO_PHOTOS);
        }
        Album album = new Album();
        BeanUtils.copyProperties(albumDTO,album);
        album.setUploadTime(LocalDateTime.now());
        this.save(album);
        //处理传过来的照片绑定相册id
        List<Photos> photos = albumDTO.getPhotos().stream()
                .peek(photo -> {
                    photo.setAlbumId(album.getId());
                    photo.setUploadTime(LocalDateTime.now());
                })
                .collect(Collectors.toList());
        photosService.saveBatch(photos);
    }

    @Override
    public void sameAlbumName(Album album) {
        String albumName = album.getName();
        if (HxsUtils.isEmpty(albumName)){
            return;
        }
        List<Album> albums = this.lambdaQuery()
                .eq(Album::getName, albumName)
                //修改的时候不查自己这一条
                .ne(HxsUtils.isNotEmpty(album.getId()),Album::getId,album.getId())
                .list();
        if (HxsUtils.isNotEmpty(albums)){
            throw new BaseException(InfoConstant.SAME_ALBUM_NAME);
        }
    }

    @Override
    public void addPhotos(AlbumDTO albumDTO) {
        List<Photos> photos = albumDTO.getPhotos();
        List<Photos> list = photos.stream().peek(p -> p.setAlbumId(albumDTO.getId())).collect(Collectors.toList());
        photosService.saveBatch(list);
    }

    @Override
    public void changeShow(Integer flag) {
        albumMapper.changeShow(flag);
    }
}
